#!/usr/bin/env bash
DIR="${BASH_SOURCE%/*}" 
FILE=$(basename "$BASH_SOURCE")
#echo $FILE

projname=wxd
dcfile=$DIR/docker-compose.deploy.yml

function do_work {
    dcfile=$1
    docker-compose -f $dcfile pull
    docker-compose -f $dcfile down
    docker-compose -f $dcfile up -d
}

if [ -v sshserver ]; then
    echo "deploy to remote:"$sshserver

    rdir="dc/$projname"
    ssh $sshserver "mkdir -p $rdir"
    scp $dcfile $sshserver:$rdir/
    scp $BASH_SOURCE $sshserver:$rdir/$FILE
    ssh $sshserver<<EOF
    cd $rdir; 
    API_BIND_ADDR=127.0.0.1 ./$FILE
EOF

else
    echo "deploy to localhost"
    do_work $dcfile
fi
